import type { MockEndpoint } from '../types'

export const logEndpoints: Record<string, MockEndpoint> = {
  '/api/logs': {
    GET: (url: string) => {
      // 解析查询参数
      const urlObj = new URL(url, 'http://localhost')
      const params = urlObj.searchParams
      const taskId = params.get('taskId')
      const status = params.get('status')
      const startDate = params.get('startDate')
      const endDate = params.get('endDate')
      
      // 所有日志数据
      const allLogs = [
        {
          id: 1,
          taskId: '1',
          taskName: '示例任务1',
          startTime: '2024-01-15T12:00:00Z',
          endTime: '2024-01-15T12:00:05Z',
          duration: 5000,
          status: 'success',
          output: 'Hello World\n命令执行成功',
          exitCode: 0
        },
        {
          id: 2,
          taskId: '2',
          taskName: '示例任务2',
          startTime: '2024-01-15T02:00:00Z',
          endTime: '2024-01-15T02:00:30Z',
          duration: 30000,
          status: 'success',
          output: '备份开始...\n备份完成',
          exitCode: 0
        },
        {
          id: 3,
          taskId: '1',
          taskName: '示例任务1',
          startTime: '2024-01-14T12:00:00Z',
          endTime: '2024-01-14T12:00:10Z',
          duration: 10000,
          status: 'error',
          output: '开始执行命令...\n发生错误',
          error: '命令执行失败: 权限不足',
          exitCode: 1
        },
        {
          id: 4,
          taskId: '2',
          taskName: '示例任务2',
          startTime: '2024-01-13T02:00:00Z',
          endTime: null,
          duration: null,
          status: 'running',
          output: '正在执行备份...',
          exitCode: null
        }
      ]
      
      // 根据查询参数筛选日志
      let filteredLogs = allLogs
      
      if (taskId) {
        filteredLogs = filteredLogs.filter(log => log.taskId === taskId)
      }
      
      if (status) {
        filteredLogs = filteredLogs.filter(log => log.status === status)
      }
      
      if (startDate) {
        filteredLogs = filteredLogs.filter(log => log.startTime >= startDate + 'T00:00:00Z')
      }
      
      if (endDate) {
        filteredLogs = filteredLogs.filter(log => log.startTime <= endDate + 'T23:59:59Z')
      }
      
      return {
        code: 0,
        msg: '操作成功',
        data: {
          logs: filteredLogs,
          total: filteredLogs.length
        }
      }
    }
  },
  '/api/logs/tasks/:taskId': {
    GET: (url: string) => {
      const taskId = url.split('/').pop()
      
      // 根据taskId筛选日志
      const allLogs = [
        {
          id: 1,
          taskId: '1',
          taskName: '示例任务1',
          startTime: '2024-01-15T12:00:00Z',
          endTime: '2024-01-15T12:00:05Z',
          duration: 5000,
          status: 'success',
          output: 'Hello World\n命令执行成功',
          exitCode: 0
        },
        {
          id: 3,
          taskId: '1',
          taskName: '示例任务1',
          startTime: '2024-01-14T12:00:00Z',
          endTime: '2024-01-14T12:00:10Z',
          duration: 10000,
          status: 'error',
          output: '开始执行命令...\n发生错误',
          error: '命令执行失败: 权限不足',
          exitCode: 1
        }
      ]
      
      const taskLogs = allLogs.filter(log => log.taskId === taskId)
      
      return {
        code: 0,
        msg: '操作成功',
        data: taskLogs
      }
    }
  },
  '/api/logs/cleanup': {
    POST: () => ({
      code: 0,
      msg: '清理完成',
      data: {
        deleted: 10,
        cutoff: '2024-01-01T00:00:00Z'
      }
    })
  }
} 